#pragma once

#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <atomic>
#include <fstream>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec);
        }

        // 获取毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec * 1000 + tv.tv_usec / 1000);
        }
    };

    const std::string temp_path = "./temp/";

    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string path_name = temp_path;
            path_name += file_name;
            path_name += suffix;

            return path_name;
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string CompilerError(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0)
            {
                // 获取属性成功，文件存在
                return true;
            }
            return false;
        }

        static std::string UniqueFileName()
        {
            std::string ms = TimeUtil::GetTimeMs();
            std::atomic_uint id(0);
            id++;
            return ms + '_' + std::to_string(id);
        }

        static bool WriteFile(const std::string &path_name, const std::string &content)
        {
            std::ofstream ofs(path_name);
            if (!ofs.is_open())
                return false;
            ofs << content;
            return true;
        }

        static bool ReadFile(const std::string &path_name, std::string &content, bool keep_line_break = false)
        {
            content.clear();

            std::ifstream ifs(path_name);
            if (!ifs.is_open())
                return false;

            std::string line;
            // getline:不保存行分割符,有些时候需要保留\n,
            // getline内部重载了强制类型转化
            while (getline(ifs, line))
            {
                content += line;
                content += (keep_line_break ? "\n" : "");
            }
            return true;
        }
    };
    class StringUtil
    {
    public:
        static void SpiltString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
            // boost split
            boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}